package com.z3n.entities.controller;

import com.ibm.jpa.web.JPAManager;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import com.ibm.jpa.web.NamedQueryTarget;
import com.ibm.jpa.web.Action;
import com.z3n.entities.Z3nUser;
import java.util.List;
import javax.persistence.Query;

@SuppressWarnings("unchecked")
@JPAManager(targetEntity = com.z3n.entities.Z3nUser.class)
public class Z3nUserManager {

	private EntityManagerFactory emf;

	public Z3nUserManager() {
	
	}

	public Z3nUserManager(EntityManagerFactory emf) {
		this.emf = emf;
	}

	public void setEntityManagerFactory(EntityManagerFactory emf) {
		this.emf = emf;
	}

	private EntityManager getEntityManager() {
		if (emf == null) {
			throw new RuntimeException(
					"The EntityManagerFactory is null.  This must be passed in to the constructor or set using the setEntityManagerFactory() method.");
		}
		return emf.createEntityManager();
	}

	@Action(Action.ACTION_TYPE.CREATE)
	public String createZ3nUser(Z3nUser Z3nUser) throws Exception {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(Z3nUser);
			em.getTransaction().commit();
		} catch (Exception ex) {
			try {
				if (em.getTransaction().isActive()) {
					em.getTransaction().rollback();
				}
			} catch (Exception e) {
				ex.printStackTrace();
				throw e;
			}
			throw ex;
		} finally {
			em.close();
		}
		return "";
	}

	@Action(Action.ACTION_TYPE.DELETE)
	public String deleteZ3nUser(Z3nUser Z3nUser) throws Exception {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Z3nUser = em.merge(Z3nUser);
			em.remove(Z3nUser);
			em.getTransaction().commit();
		} catch (Exception ex) {
			try {
				if (em.getTransaction().isActive()) {
					em.getTransaction().rollback();
				}
			} catch (Exception e) {
				ex.printStackTrace();
				throw e;
			}
			throw ex;
		} finally {
			em.close();
		}
		return "";
	}

	@Action(Action.ACTION_TYPE.UPDATE)
	public String updateZ3nUser(Z3nUser Z3nUser) throws Exception {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Z3nUser = em.merge(Z3nUser);
			em.getTransaction().commit();
		} catch (Exception ex) {
			try {
				if (em.getTransaction().isActive()) {
					em.getTransaction().rollback();
				}
			} catch (Exception e) {
				ex.printStackTrace();
				throw e;
			}
			throw ex;
		} finally {
			em.close();
		}
		return "";
	}

	@Action(Action.ACTION_TYPE.FIND)
	public Z3nUser findZ3nUserByUserId(int userId) {
		Z3nUser Z3nUser = null;
		EntityManager em = getEntityManager();
		try {
			Z3nUser = (Z3nUser) em.find(Z3nUser.class, userId);
		} finally {
			em.close();
		}
		return Z3nUser;
	}

	@Action(Action.ACTION_TYPE.NEW)
	public Z3nUser getNewZ3nUser() {
	
		Z3nUser Z3nUser = new Z3nUser();
	
		return Z3nUser;
	}

	@NamedQueryTarget("getZ3nUserByUserPassword")
	public List<Z3nUser> getZ3nUserByUserPassword(String userPassword) {
		EntityManager em = getEntityManager();
		List<Z3nUser> results = null;
		try {
			Query query = em.createNamedQuery("getZ3nUserByUserPassword");
			query.setParameter("userPassword", userPassword);
			results = (List<Z3nUser>) query.getResultList();
		} finally {
			em.close();
		}
		return results;
	}

	@NamedQueryTarget("getZ3nUserByUserName")
	public List<Z3nUser> getZ3nUserByUserName(String userName) {
		EntityManager em = getEntityManager();
		List<Z3nUser> results = null;
		try {
			Query query = em.createNamedQuery("getZ3nUserByUserName");
			query.setParameter("userName", userName);
			results = (List<Z3nUser>) query.getResultList();
		} finally {
			em.close();
		}
		return results;
	}

	@NamedQueryTarget("getZ3nUserByUserEmail")
	public List<Z3nUser> getZ3nUserByUserEmail(String userEmail) {
		EntityManager em = getEntityManager();
		List<Z3nUser> results = null;
		try {
			Query query = em.createNamedQuery("getZ3nUserByUserEmail");
			query.setParameter("userEmail", userEmail);
			results = (List<Z3nUser>) query.getResultList();
		} finally {
			em.close();
		}
		return results;
	}

	@NamedQueryTarget("getZ3nUserByUserAddress")
	public List<Z3nUser> getZ3nUserByUserAddress(String userAddress) {
		EntityManager em = getEntityManager();
		List<Z3nUser> results = null;
		try {
			Query query = em.createNamedQuery("getZ3nUserByUserAddress");
			query.setParameter("userAddress", userAddress);
			results = (List<Z3nUser>) query.getResultList();
		} finally {
			em.close();
		}
		return results;
	}

	@NamedQueryTarget("getZ3nUserByUserActivitationurl")
	public List<Z3nUser> getZ3nUserByUserActivitationurl(
			String userActivitationurl) {
		EntityManager em = getEntityManager();
		List<Z3nUser> results = null;
		try {
			Query query = em
					.createNamedQuery("getZ3nUserByUserActivitationurl");
			query.setParameter("userActivitationurl", userActivitationurl);
			results = (List<Z3nUser>) query.getResultList();
		} finally {
			em.close();
		}
		return results;
	}

	@NamedQueryTarget("getZ3nUserByUserId")
	public List<Z3nUser> getZ3nUserByUserId(int userId) {
		EntityManager em = getEntityManager();
		List<Z3nUser> results = null;
		try {
			Query query = em.createNamedQuery("getZ3nUserByUserId");
			query.setParameter("userId", userId);
			results = (List<Z3nUser>) query.getResultList();
		} finally {
			em.close();
		}
		return results;
	}

}